package com.gitee.kenewstar.migration.util;

import com.alibaba.excel.EasyExcelFactory;
import com.gitee.kenewstar.migration.entity.CommonDto;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * excel参考链接：https://easyexcel.opensource.alibaba.com/docs/current/
 * @author kenewstar
 */
public class ExcelUtil {

    private ExcelUtil() {}

    public static <T> List<T> readData(MultipartFile file, Class<T> clz) {
        try {
            return EasyExcelFactory.read(file.getInputStream())
                    .head(clz)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
    
    public static <T extends CommonDto> List<T> filterReadData(MultipartFile file, Class<T> clz) {
        try {
            List<T> list = EasyExcelFactory.read(file.getInputStream())
                    .head(clz)
                    .sheet()
                    .doReadSync();
            return list.stream().filter(CommonDto::completeFilter).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }


}
